package com.community.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.community.entity.User;
import com.community.service.IUserService;

@Controller
@RequestMapping("/user/login")
public class UserLoginController {
	
	@Autowired
	private IUserService iUserService;
	
	@RequestMapping()
	private String userLogin(HttpSession session,ModelMap modelMap){
		if(session.getAttribute("userid") == null){
			return "/user/login";
		}else{
			Long id = Long.valueOf(session.getAttribute("userid").toString());
			User u = iUserService.getUser(id);
			modelMap.addAttribute("user", u);
			return "/user/user_center";
		}
	}

	@RequestMapping("/check")
	public String loginCheck(String count,String password,ModelMap modelMap,HttpSession session){
		if(session.getAttribute("userid") == null){
			User u = iUserService.getUserByCountAndPassword(count, password);
			if(u == null){
				return "/user/login";
			}else{
				session.setAttribute("userid", u.getId());
				modelMap.addAttribute("user", u);
				
				return "/user/user_center";
			}
		}else{
			return "/user/login";
		}
	}
	
	@RequestMapping("/out")
	public String loginOut(ModelMap modelMap,HttpSession session){
		session.removeAttribute("userid");
		return "/user/login";
	}
	/**
	 * 检测用户是否登录
	 * @return
	 */
	@RequestMapping("/isLogin")
	@ResponseBody
	public int isLogin(HttpSession session){
		if(session.getAttribute("userid") == null){
			return 0;
		}else{
			return 1;
		}
	}
}
